<?php

namespace app\admin\controller;

use think\Db;
use app\admin\controller\Base;

class Costs extends Base
{
    public function index()
    {
        $DB = Db::name("costs")->field(["id", "lever", "overnight_fee", "bond", "service_charge"])->paginate(10);
        $this->assign("query", $DB);
        return $this->fetch('list');
    }

    public function add()
    {
        if (isset($_POST["sub"])) {
            $data['lever'] = input("lever");
            $data['overnight_fee'] = input("overnight_fee");
            $data['bond'] = input("bond");
            $data['service_charge'] = input("service_charge");
            $DB = Db::name('costs')->insert($data);
            if ($DB) {
                $this->success("操作成功", "/admin/cost/list");
            } else {
                $this->error("操作失败", "/admin/cost/list");
            }
        } else {
            return $this->fetch("insert");
        }
    }

    public function update($id)
    {
        if (isset($_POST["update"])) {
            $id=intval(input("id"));
            $data['lever'] = input("lever");
            $data['overnight_fee'] = input("overnight_fee");
            $data['bond'] = input("bond");
            $data['service_charge'] = input("service_charge");
            $DB = Db::name('costs')->where('id',$id)->update($data);
            if ($DB) {
                $this->success("操作成功", "/admin/cost/list");
            } else {
                $this->error("操作失败", "/admin/cost/list");
            }
        } else {
            $id = intval($id);
            $query = Db::name('costs')->where('id',$id)->field(["id", "lever", "overnight_fee", "bond", "service_charge"])->find();
            $this->assign("query",$query);
            return $this->fetch("update");
        }
    }
}
















































